<template>
  <div class="app-container flex flex-col">
    <div class="app-container__section">
      <plus-search
          v-bind="searchBinds"
          :columns="columns"
      ></plus-search>
      <div></div>
    </div>

    <div class="app-container__section">
      <plus-table
          v-bind="tableBinds"
          :columns="columns"
      >
        <template #title>
          <table-title />
        </template>
        <template #plus-cell-dictType="{ row }">
          <router-link class="link-type" :to="`/system/dict-data/index/${row.dictId}`">{{row.dictType}}</router-link>
        </template>
        <template #toolbar>
          <el-button
              type="primary"
              icon="Plus"
              @click="handleAdd"
              v-hasPermi="['system:dict:add']"
          >
            新增
          </el-button>
          <el-button
              type="danger"
              icon="Delete"
              :disabled="gSelection.length === 0"
              @click="handleDelete(gSelection)"
              v-hasPermi="['system:dict:remove']"
          >
            删除
          </el-button>
          <el-button
              type="danger"
              icon="Refresh"
              @click="handleRefreshCache"
              v-hasPermi="['system:dict:remove']"
          >
            刷新缓存
          </el-button>
          <ClockTimer :task="gGetList"/>
          <SearchAreaControl v-model="gShowSearchForm"/>
        </template>
        <template #plus-cell-action="scope">
          <el-tooltip effect="dark" content="修改" placement="top">
            <el-button
                type="primary"
                link
                icon="EditPen"
                @click="handleUpdate(scope.row)"
            >
            </el-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="删除" placement="top">
            <el-button
                type="danger"
                link
                icon="Delete"
                @click="handleDelete(scope.row)"
            >
            </el-button>
          </el-tooltip>
        </template>
      </plus-table>
    </div>

    <!-- 添加或修改参数配置对话框 -->
    <addDictType ref="addFormRef" @refreshList="gGetList" />

  </div>
</template>

<script setup >
import { useCommonConfig,useColumnsDict} from './hook.js'
import { useDelHook, useListHook } from '@/hooks/crud/index.js'
import { useListConfigHook } from '@/hooks/usePlusConfigHook.js'
import addDictType from './addDictType.vue'
import { refreshCache } from '@/api/system/config.js'
defineOptions({
  name: 'Dict'
})
const { proxy,apiFn,primaryKey,moduleName,beforeDelFn } = useCommonConfig()
const { dictData, columns, } = useColumnsDict()

// ====================列表逻辑=========================
const gSearchForm = ref({ })
const { gGetList, gAllExposeOption, } = useListHook({
  listApiFn: apiFn.listApiFn,
  gSearchForm,
  beforeRequest: (reqData) => {
    return reqData
  },
  afterRequest: (resData) => {
    return resData
  }
})
// ====================属性绑定=========================
const { gTableBinds, gSearchBinds, gSelection, gShowSearchForm } = useListConfigHook()
const tableBinds = computed(() => gTableBinds({ ...gAllExposeOption,primaryKey }))
const searchBinds = computed(() => gSearchBinds({ ...gAllExposeOption } ))
// ==================删除逻辑===========================
const { gDelete } = useDelHook({
  delApiFn: apiFn.delApiFn,
  moduleName,
  beforeDelFn: (reqData) => beforeDelFn(reqData),
  afterDelFn: (resData) =>gGetList(),
})
const handleDelete = (data)=>{
  gDelete(data)
}
// ===================新增、修改======================
const addFormRef = ref(null)
// 新增操作
function handleAdd() {
  addFormRef.value.gOpen()
}
// 修改操作
function handleUpdate(row) {
  addFormRef.value.gOpen(row)
}

function handleRefreshCache() {
  refreshCache().then(() => {
    proxy.$modal.msgSuccess("刷新成功");
  });
}

</script>
